package pri.jz.keeper.jui.jsheet

import android.util.Log
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.launch
import pri.jz.keeper.ui.theme.Grey2
import java.util.Arrays

/**
 *Author by: JZ
 *Email: 2451756617@qq.com
 *PS:
 */
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun JBottomSelectSheet(
    sheetState: ModalBottomSheetState,
    itemList: List<String>,
    onCancel: () -> Unit,
    onConfirm: (Int) -> Unit,
    content: @Composable () -> Unit
) {
    ModalBottomSheetLayout(
        sheetState = sheetState,
        sheetShape = RoundedCornerShape(topStart = 15.dp, topEnd = 15.dp),
        sheetContent = {
            var selectIndex = remember{ 0 }
            Column {
                Row(
                    modifier = Modifier.fillMaxWidth()
                ) {
                    Text(
                        "取消",
                        modifier = Modifier
                            .padding(pri.jz.keeper.ui.view.HorizontalPadding)
                            .clickable(onClick = onCancel),
                        color = Color.Black,
                        fontSize = 16.sp
                    )
                    Spacer(Modifier.weight(1f))
                    Text(
                        "确认",
                        modifier = Modifier
                            .padding(
                                pri.jz.keeper.ui.view.HorizontalPadding
                            )
                            .clickable(onClick = {onConfirm(selectIndex)}),
                        color = Color.Red,
                        fontSize = 16.sp
                    )
                }
                JSelectLazyList(itemList) {
                    selectIndex = it
                }
            }
        },
        content = content
    )
}

@Composable
private fun JSelectLazyList(
    itemList: List<String>,
    onSelectChange: (Int) -> Unit
) {
    val list = remember { mutableListOf(" ").apply {
        addAll(itemList)
        add(" ")
    } }
    val state = rememberLazyListState()
    var listIndex by remember { mutableStateOf(1) }
    LaunchedEffect(state) {
        snapshotFlow { state.firstVisibleItemScrollOffset }.collect {
            if (state.firstVisibleItemIndex + 1 != listIndex) {
                listIndex = state.firstVisibleItemIndex + 1
                onSelectChange(listIndex - 1)
            }
        }
    }
    LazyColumn(
        modifier = Modifier.fillMaxWidth().height(130.dp),
        state = state
    ) {
        itemsIndexed(list) { index, item ->
            val isSelected = listIndex == index
            Spacer(Modifier.height(10.dp))
            Text(
                item,
                fontSize = if (isSelected) 22.sp else 16.sp,
                fontWeight = if (isSelected) FontWeight.Bold else null,
                color = if (isSelected) Color.Black else Grey2,
                modifier = Modifier.fillMaxWidth(),
                textAlign = TextAlign.Center
            )
            Spacer(Modifier.height(10.dp))
        }
    }
}

